package com.offcn.seckill.thread;

import com.offcn.seckill.dao.SeckillGoodsMapper;
import com.offcn.seckill.pojo.SeckillGoods;
import com.offcn.seckill.pojo.SeckillOrder;
import com.offcn.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MultiTreadeCreateOrder {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 实现多线程下单操作
     * <p>
     * username 名称
     * createtime  创建时间
     * 秒杀的状态
     */
    @Async
    public void createOrder() {

        // 从redis  中的排队信息获取
        SecKillOrderStatus secKillOrderStatus = (SecKillOrderStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();

        try {
            Object pop = redisTemplate.boundListOps("SeckillGoodsCountList_" + secKillOrderStatus.getGoodsId()).rightPop();

            if (pop==null){
                //清除用户的排队信息
                clearQueue(secKillOrderStatus);
                return;
            }
            if (secKillOrderStatus != null) {
                String time = secKillOrderStatus.getTime();
                Long id = secKillOrderStatus.getOrderId();
                String username = secKillOrderStatus.getUsername();

                //获取商品数据
                SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + time).get(id);


                if (seckillGoods == null || seckillGoods.getStockCount() <= 0) {
                    throw new RuntimeException("已经售罄");
                }

                //如果有库存 创建秒杀 订单
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(idWorker.nextId());
                seckillOrder.setSeckillId(id);
                seckillOrder.setMoney(seckillGoods.getCostPrice());
                seckillOrder.setUserId(username);
                seckillOrder.setCreateTime(new Date());
                //设立未支付
                seckillOrder.setStatus("0");

                //将秒杀订单存入redis
                redisTemplate.boundHashOps("SeckillOrder").put(username, seckillOrder);

                //减少库存
                seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
                //判断 库存减少后 是否哈有库存
                if (seckillGoods.getStockCount() <= 0) {
                    //将redsi 商品数据 同步到 数据库
                    seckillGoodsMapper.updateById(seckillGoods);
                    //清除redsi 中的缓存
                    redisTemplate.boundHashOps("SeckillGoods_" + time).delete(id);
                } else {
                    //剩余库存 把数据同步到 redis
                    redisTemplate.boundHashOps("SeckillGoods_" + time).put(id, seckillGoods);
                }

                //抢单成功的状态  更新抢购状态从排队中 变为等待支付
                secKillOrderStatus.setStatus(2);
                secKillOrderStatus.setOrderId(seckillOrder.getId());
                secKillOrderStatus.setMoney(seckillOrder.getMoney().floatValue());

                // 跟新 redis
                redisTemplate.boundHashOps("UserQueueStatus").put(username,secKillOrderStatus);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void clearQueue(SecKillOrderStatus secKillOrderStatus) {
        //清理用户的排队的标识

        redisTemplate.boundHashOps("UserQueueCount").delete(secKillOrderStatus.getUsername());
        //清除用户的抢单标识
        redisTemplate.boundHashOps("UserQueueStatus").delete(secKillOrderStatus.getUsername());

    }
}
